package com.gz.moreaseManager.modules.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.gz.moreaseManager.core.enums.YesOrNoEnum;
import com.gz.moreaseManager.core.modal.QueryMapHelper;
import com.gz.moreaseManager.modules.entity.Banner;
import com.gz.moreaseManager.modules.entity.MenuWeb;
import com.gz.moreaseManager.modules.entity.ProductTag;
import com.gz.moreaseManager.modules.mapper.MenuWebMapper;
import com.gz.moreaseManager.modules.service.IBannerService;
import com.gz.moreaseManager.modules.service.IMenuWebService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.modules.service.IProductTagService;
import com.gz.moreaseManager.modules.transfer.MenuWebVo;
import com.gz.moreaseManager.sys.entity.Dict;
import com.gz.moreaseManager.sys.service.IDictService;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 官网菜单表 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-25
 */
@Service
public class MenuWebServiceImpl extends ServiceImpl<MenuWebMapper, MenuWeb> implements IMenuWebService {

    @Resource
    private MenuWebMapper menuWebMapper;

    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IDictService dictService;
    @Autowired
    private IProductTagService productTagService;

    @Override
    public List<MenuWebVo> findIndexMenuList() {
        Wrapper menuQuery = new EntityWrapper<MenuWeb>();
        //只取状态显示的菜单
        menuQuery.eq("status", YesOrNoEnum.Y.getCode());//显示
        menuQuery.orderBy("sort");
        List<MenuWeb> menuWebList = menuWebMapper.selectList(menuQuery);
        List<MenuWebVo> menuWebVoList = null;
        if(UtilTool.isNotBlank( menuWebList )){
            menuWebVoList = packingMenus(0,menuWebList);
        }
        //根据一级菜单获取轮播图信息
        if(UtilTool.isNotBlank( menuWebVoList )){
            //获取字典内分配的菜单对照关系
            Map<String,String> menuBannerMap = dictService.findMapToCodeByCode("menuBanner");
            //获取轮播图列表信息 类型位置作为key
            Map<String,Banner> bannerMap = bannerService.findAllMapBanner();
            if( UtilTool.isNotBlank(menuBannerMap)){
                for( MenuWebVo menuWebVo: menuWebVoList){
                    //"4":"品牌二级导航栏","5":"产品二级导航栏","6":"合作二级导航栏","7":"资讯二级导航栏","8":"会员二级导航栏"
                    String bannerType = menuBannerMap.get( menuWebVo.getAuthCode());
                    if( UtilTool.isNotBlank( bannerType )){
                        //根据类型位置返回轮播图信息
                        Banner banner = bannerMap.get( bannerType );
                        if( UtilTool.isNotBlank( banner )){
                            //获取轮播图对应的封面图片路径和名称
                            menuWebVo.setBannerUrl( banner.getBannerUrl());
                            menuWebVo.setBannerTitle( banner.getBannerName() );
                            menuWebVo.setLink(banner.getBannerLink());//超链接取轮播图对应连接
                        }
                    }
                    //产品获取方式不一样  根据字典类别type + 标签管理获取
                    if( "pro".equals(menuWebVo.getAuthCode())){
                        productMenuData(menuWebVo);
                    }
                }
            }
            return menuWebVoList;
        }

        return null;
    }

    /**
     * 封装产品类别的菜单
     * @param menuWebVo
     */
    private void productMenuData(MenuWebVo menuWebVo){

        //1.获取产品标签的类别
        List<Dict> dictList = dictService.findChildDict("type");
        //2.获取所有的标签列表
        List<ProductTag> tagList =productTagService.selectList(null);

        List<MenuWebVo> childMenuWeb = new ArrayList<>();
        if( UtilTool.isNotBlank( dictList )){
            for( Dict dict : dictList ){
                MenuWebVo menuWebVoChild = new MenuWebVo();
                menuWebVoChild.setAuthCode( dict.getCode() );
                menuWebVoChild.setId( dict.getId() );
                menuWebVoChild.setName( dict.getName() );
                menuWebVoChild.setPid( menuWebVo.getId());
                List<MenuWebVo> childMenu = new ArrayList<>();
                if( UtilTool.isNotBlank( tagList )){
                    for(ProductTag tag : tagList ){
                        if( tag.getType().equals( dict.getId() )){
                            MenuWebVo childVo = new MenuWebVo();
                            childVo.setAuthCode( tag.getName() );
                            childVo.setId( tag.getId() );
                            childVo.setName( tag.getName() );
                            childVo.setPid( dict.getId() );

                            childMenu.add( childVo );
                        }
                    }
                    menuWebVoChild.setChildMenu(childMenu);
                }
                childMenuWeb.add( menuWebVoChild );
            }
            menuWebVo.setChildMenu(childMenuWeb);
        }
    }

    /**
     * 递归打包菜单
     *
     * @param pid   父菜单ID
     * @param sourceMenu 用户权限
     * @return 菜单结构集
     */
    private List<MenuWebVo> packingMenus(Integer pid, List<MenuWeb> sourceMenu) {
        List<MenuWeb> remainMenu;
        List<MenuWebVo> menuList = new ArrayList<>();
        remainMenu = new ArrayList<>(sourceMenu);
        for (MenuWeb menuInfo : sourceMenu) {
            if ( pid == menuInfo.getPid() ) {

                MenuWebVo menuWebVo = new MenuWebVo();
                BeanUtils.copyProperties(menuInfo,menuWebVo);

                remainMenu.remove(menuInfo);
                menuWebVo.setChildMenu(packingMenus(menuInfo.getId(), remainMenu));
                menuList.add(menuWebVo);
            }
        }
        return menuList;
    }

    /**
     * 获取ID+auth_code 的map
     * @return
     */
    public Map<Integer,String> menuWebAuthCodeMap(){

        Map<Integer,String> authCodeMap  = QueryMapHelper.toMap(menuWebMapper.selectAuthCodeMapList());
        return authCodeMap;
    }
}
